<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link href="/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css"
      th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-edit" th:object="${Role}">
        <input id="roleId" name="roleId" type="hidden" th:field="*{roleId}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label ">角色名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" readonly="readonly" name="roleName" id="roleName"
                       th:field="*{roleName}"/>
            </div>
        </div>

        <div hidden="hidden" class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <div class="onoffswitch">
                    <input type="checkbox" readonly="readonly" th:checked="*{status == 0 ? true : false}"
                           class="onoffswitch-checkbox" id="status" name="status">
                    <label class="onoffswitch-label" for="status">
                        <span class="onoffswitch-inner"></span>
                        <span class="onoffswitch-switch"></span>
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">菜单权限</label>
            <div class="col-sm-8">
                <div id="menuTrees" class="ztree"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="form-control-static col-sm-offset-9">
                <button type="submit" class="btn btn-primary">提交</button>
                <button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js}"></script>
<script type="text/javascript">



    // 表单校验
    $("#form-role-edit").validate({
        submitHandler: function (form) {
            btn.save();
        }
    });

    //初始化加载
    $(function () {
        btn.treeMenu();
        btn.selectNodes($("#roleId").val());
    })

    var btn = {
        treeMenu: function () {
            var zTreeObj,
                setting = {
                    view: {
                        selectedMulti: false
                    },
                    callback: {
                        beforeCheck: true,
                        onCheck: btn.onCheck
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",    //复选框
                        chkboxType: {"Y": "p", "N": "s"}
                        // 被勾选时：关联父  关联子√
                        // 取消勾选时：关联父√  关联子
                    }

                },
                zTreeNodes = btn.initDeptData();
            zTreeObj = $.fn.zTree.init($("#menuTrees"), setting, zTreeNodes);
        },
        //点击树状复选框回调
        onCheck: function (e, treeId, treeNode) {
            var menuIds = [];
            var treeObj = $.fn.zTree.getZTreeObj("menuTrees"),
                nodes = treeObj.getCheckedNodes(true),
                v = "";
            for (var i = 0; i < nodes.length; i++) {
                menuIds.push(nodes[i].id);
            }
            return menuIds;

        },

        //请求服务端获取数据菜单
        initDeptData: function () {
            var array1;

            $.ajaxSettings.async = false;
            $.post("/menu/ajaxlist", {}, function (data) {
                array1 = data;
            })
            return array1;
        },

        //提交表单
        save: function () {
            var roleId = $("input[name='roleId']").val();
            var roleName = $("input[name='roleName']").val();
            var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
            $.ajax({
                cache: true,
                type: "POST",
                url: "/role/editPowerSave",
                traditional: true,
                data: {
                    "roleId": roleId,
                    "roleName": roleName,
                    "status": status,
                    "ids": btn.onCheck()
                },
                async: true,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    $.operate.saveSuccess(data);
                }
            });
        },

        //遍历所有节点初始化选中
        selectNodes: function (roleId) {
            var treeObj = $.fn.zTree.getZTreeObj("menuTrees");
            var nodesAll = treeObj.getNodes(); //可以获取所有的节点
            var nodesArray = treeObj.transformToArray(nodesAll); // 将数据变成简单的 Array 集合
            $.post("/role/selectById/" + roleId, {}, function (data) {
                var powers = data.permissionList;
                var node2="";
                for (var i = 0; i < powers.length; i++) {
                    for (var j = 0; j < nodesArray.length; j++) {
                        if (nodesArray[j].id != null) {
                            debugger
                            if (powers[i].permissionId == nodesArray[j].id) {
                                node2 = treeObj.getNodeByParam("id", nodesArray[j].id);//根据ID找到该节点
                                treeObj.checkNode(node2,true,true);//根据该节点选中
                                treeObj.expandNode(node2, true, false);//指定选中ID节点展开
                                treeObj.updateNode(node2);
                            }
                        }
                    }
                }
            })
        }
    }


</script>
</body>
</html>
